In [1]:
import pandas as pd
In [2]:
df = pd.read_csv("C:/Users/ASUS/OneDrive/Desktop/DatasetR/houseprice.csv")
df
Out[2]:
area price
0 2600 550000
1 3000 565000
2 3200 610000
3 3600 680000
4 4000 725000
In [3]:
import plotly.express as px
import plotly.graph_objects as go
fig = px.scatter(df, x="area", y="price",  
                 labels={"area": "Area (sq ft)",
                     "price": "Price (USD)"},
                title="Housing Price")
fig.show()
In [4]:
from sklearn import linear_model
In [5]:
model_obj = linear_model.LinearRegression()
model_obj.fit(df[['area']],df.price)
Out[5]:
LinearRegression()
In [6]:
model_obj.predict([[3300]])
Out[6]:
array([628715.75342466])
In [7]:
# Linear Regression y = mx + b
# Value of m that is slope or gradient
model_obj.coef_
Out[7]:
array([135.78767123])
In [8]:
model_obj.intercept_
Out[8]:
180616.43835616432
In [9]:
# y = mx + b
135.78767123*3300 + 180616.43835616432
Out[9]:
628715.7534151643
In [10]:
#Using plotly.express's trendline parameter to train a simply Ordinary Least Square (OLS) for predicting the Price

fig = px.scatter(df, x="area", y="price",  
                 labels={"area": "Area (sq ft)",
                     "price": "Price (USD)"},
                title="Housing Price", opacity=0.65,
    trendline='ols', trendline_color_override='darkblue')
fig.show()
In [ ]:
#Gradient Descent is iterative while OLS isn't.
#Gradient Descent uses a learning rate to reach the point of minima, 
#while OLS just finds the minima of the equation using partial differentiation.
In [ ]: